package com.hn.shopadmin.controller;

import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.result.CommonPage;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import com.hn.kiss.modules.sys.shopdb.entity.Role;
import com.hn.shopadmin.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "RoleController", description = "角色管理")
@RestController
@RequestMapping("/role")
@Validated
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("角色添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "discribe", value = "角色描述", dataType = "String", paramType = "query")
    })
    @PostMapping("/insert")
    public CommonResult inset(@NotBlank(message = "角色代码不能为空") String code,
                              @NotBlank(message = "角色名称不能为空") String name,
                              String discribe) throws ParmException {
        int count = roleService.insert(code, name, discribe);
        if (count > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("保存失败");
    }

    @ApiOperation("启用的角色列表")
    @GetMapping("/listAll")
    public CommonResult<List<Role>> listAll(){
        List<Role> roles = roleService.listALlRole();
        return CommonResult.success(roles);
    }

    @ApiOperation("角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "角色代码/名称/描述", dataType = "String", paramType = "query")
    })
    @GetMapping("/listpage")
    public CommonPage<Role> listpage(@NotNull(message = "page不能为空") Integer page,
                                     @NotNull(message = "limit不能为空") Integer limit,
                                     String keywork){
        PageInfo<Role> rolePageInfo = roleService.listPageRole(page, limit, keywork);
        return CommonPage.restPage(rolePageInfo);
    }

    @ApiOperation("角色信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "discribe", value = "角色描述", dataType = "String", paramType = "query")
    })
    @PostMapping("/update")
    public CommonResult update(@NotBlank(message = "角色代码不能为空") String code,
                               @NotBlank(message = "角色名称不能为空") String name,
                               String discribe) throws ParmException {
        int count = roleService.update(code, name, discribe);
        if (count > 0){
            return CommonResult.success("角色信息修改成功");
        }
        return  CommonResult.failed("角色信息修改失败");
    }

    @ApiOperation("角色停用")
    @ApiImplicitParam(name = "code", value = "角色代码", required = true, dataType = "String", paramType = "query")
    @PostMapping("/stop")
    public CommonResult stop(@NotBlank(message = "角色代码不能为空") String code){
        int count = roleService.stop(code);
        if (count > 0){
            return CommonResult.success("角色停用成功");
        }
        return  CommonResult.failed("角色停用失败");
    }

    @ApiOperation("角色启用")
    @ApiImplicitParam(name = "code", value = "角色代码", required = true, dataType = "String", paramType = "query")
    @PostMapping("/start")
    public CommonResult start(@NotBlank(message = "角色代码不能为空") String code){
        int count = roleService.start(code);
        if (count > 0){
            return CommonResult.success("角色启用成功");
        }
        return  CommonResult.failed("角色启用失败");
    }



}
